<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\model\Address as AddressModel;
/**
 * 地址
 */
class Address extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 列表
     * @ApiMethod (GET)
     */
    public function list()
    {

        $data = AddressModel::where(['user_id' => $this->auth->id])
                                                ->order('is_moren', 'desc')
                                                ->select();
        return $this->success('OK', $data);
    }

    /**
     * 选择默认收货地址
     * @ApiMethod (POST)
     */
    public function moren()
    {
        $id = input('id');
        if(input('is_moren') == 2)
        {
            $res = AddressModel::where(['user_id' => $this->auth->id, 'is_moren' => 2])
                                                ->find();
            if($res)
            {
                $res->updatetime = time();
                $res->is_moren     = 1;
                $res->save();
            }
            $res = AddressModel::where('id', $id)->update(['is_moren' => 2]);
        }else{
            $res = AddressModel::where('id', $id)->update(['is_moren' => 1]);

        }
        if($res)
        {
            return $this->success('OK', '设置成功');
        }
        return $this->error('服务器繁忙，请稍后再试');

    }
     /**
     * 修改收货地址
     * @ApiMethod (POST)
     */
    public function edit()
    {
        $id = input('id');
        $input = input('post.');
        if(input('is_moren') == 2)
        {
            $res = AddressModel::where(['user_id' => $this->auth->id, 'is_moren' => 2])
                                                ->find();
            if($res)
            {
                $res->updatetime = time();
                $res->is_moren     = 1;
                $res->save();
            }
        }
        $res = AddressModel::update($input);
        if($res)
        {
            return $this->success('OK', '修改成功');
        }
        return $this->error('服务器繁忙，请稍后再试');


    }

     /**
     * 新增收货地址
     * @ApiMethod (POST)
     */
    public function add()
    {

        $input = input('post.');
        $input['user_id'] = $this->auth->id;
        if(input('is_moren') == 2)
        {
            $res = AddressModel::where(['user_id' => $this->auth->id, 'is_moren' => 2])
                                                ->find();
            if($res)
            {
                $res->updatetime = time();
                $res->is_moren     = 1;
                $res->save();
            }
        }
        $res = AddressModel::create($input);
        if($res)
        {
            return $this->success('OK', '添加成功');
        }
        return $this->error('服务器繁忙，请稍后再试');

    }

    /**
     * 删除收货地址
     * @ApiMethod (POST)
     */
    public function del()
    {
        $res = AddressModel::where('id', input('id'))
                                ->delete();
        if($res)
        {
            return $this->success('OK', '删除成功');
        }
        return $this->error('服务器繁忙，请稍后再试');

    }
}
